#include "mvc_delegate.h"
#include <QLineEdit>


#define Affirm2(text)    QMessageBox::question(NULL,trUtf8("输入非法！"),trUtf8(text),\
                          QMessageBox::Ok|QMessageBox::Cancel)==QMessageBox::Ok

Delegate::Delegate(QObject *parent)
    : QItemDelegate(parent)
{
}

QWidget *Delegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem & option ,
    const QModelIndex & index ) const
{
    if( index.column()==0)
    {
     QLabel *label = new QLabel(parent);
     return label;
    }
    else
    {
        QSpinBox *editor = new QSpinBox(parent);
        set_input_range(editor,index);
        return editor;
    }

}


void Delegate::setEditorData(QWidget *editor,
                                    const QModelIndex &index) const
{
    if( index.column()==1)
    {
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    spinBox->setValue(index.model()->data(index, Qt::EditRole).toInt());
    }
}


void Delegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                   const QModelIndex &index) const
{
    if( index.column()==1)
    {
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    model->setData(index, spinBox->value(), Qt::EditRole);
    }
}

void Delegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
    editor->setGeometry(option.rect);

}

void Delegate::set_input_range(QSpinBox *spinBox,const QModelIndex &index) const
{
    QString keystring=keylist[index.row()];

    if( keystring==trUtf8("灭菌温度"))
     {
        if(checktype==1)
       {
          spinBox->setRange(100,140);
       }
        else if(checktype==2)
       {
         spinBox->setRange(100,130);
       }
     }
    else  if( keystring==trUtf8("灭菌时间")||
//              keystring==trUtf8("脉动次数")||
//              keystring==trUtf8("干燥次数")||
              keystring==trUtf8("干燥时间")||
              keystring==trUtf8("预平衡时间")

              )
    {
      spinBox->setRange(0,0x10000);
    }
    else if( keystring==trUtf8("干燥压力")||
             keystring==trUtf8("脉动压力下限")
             )
    {
      spinBox->setRange(-90,0);
    }
    else if( keystring==trUtf8("脉动压力上限")||
             keystring==trUtf8("排冷压力"))
    {
       spinBox->setRange(0,140);
    }
    else if( keystring==trUtf8("恒温上限"))
    {
      spinBox->setRange(9,15);
    }
    else if( keystring==trUtf8("恒温下限"))
    {
      spinBox->setRange(5,9);
    }
    else spinBox->setRange(-100,10000);
}
/*
bool Delegate::input_check(const QModelIndex &index,int value) const
{
    bool retbool;
    QString keystring=keylist[index.row()];


    if( keystring==trUtf8("灭菌温度"))
     {
        if(checktype==1)
       {
        if(value>140 || value<100) {Affirm2("有效值为100~140");retbool=false;}
        else retbool=true;
       }
        else if(checktype==2)
       {
        if(value>130 || value<100) {Affirm2("有效值为100~130");retbool=false;}
        else retbool=true;
       }
     }
    else  if( keystring==trUtf8("灭菌时间")||
              keystring==trUtf8("脉动次数")||
              keystring==trUtf8("干燥次数")||
              keystring==trUtf8("干燥时间")||
              keystring==trUtf8("预平衡时间")||
              keystring==trUtf8("排冷")
              )
    {
       if( value< 0) {Affirm2("不能为负");retbool=false;}
       else retbool=true;
    }
    else if( keystring==trUtf8("干燥压力")||
             keystring==trUtf8("脉动压力下限")
             )
    {
       if(value>0 || value<-100) {Affirm2("有效值为-90~0");retbool=false;}
       else retbool=true;
    }
    else if( keystring==trUtf8("脉动压力上限"))
    {
       if(value>200 || value<0) {Affirm2("有效值为0~200");retbool=false;}
       else retbool=true;
    }


    return retbool;
}
*/
void Delegate::set_datamap(QMap<QString,int>* datamap_ptr,uint checktype)
{
    this->checktype=checktype;
    this->datamap_ptr=datamap_ptr;
     keylist=datamap_ptr->keys();
}
